package com.atguigu.service;

import com.atguigu.pojo.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Component
public class UserService implements UserDetailsService {

    static Map<String, User> map = new HashMap<>();

    static {
        User user1 = new User();
        user1.setUsername("admin");
        user1.setPassword("$2a$10$aR6PeTvA6JbpC8EQAhr..OlTspMiTx7sBhfmmgadOCgAQc7kv5bua");
        user1.setTelephone("123");

        User user2 = new User();
        user2.setUsername("zhangsan");
        user2.setPassword("$2a$10$/wCYzxZ2oXHZLumN9UAXHOhQJpJN.Uut0euuQCqm1txr94reU8z5C");
        user2.setTelephone("321");

        map.put(user1.getUsername(),user1);
        map.put(user2.getUsername(),user2);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("username"+username);
        User userInDb = map.get(username);
        if (userInDb==null){
            return null;
        }
        String passwordInDb = userInDb.getPassword();
        List<GrantedAuthority> lists = new ArrayList<>();
        lists.add(new SimpleGrantedAuthority("add"));
//        lists.add(new SimpleGrantedAuthority("delete"));
        lists.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        return new org.springframework.security.core.userdetails.User(username,passwordInDb,lists);
    }

}
